/**
 * @author BoLiang
 */
var MemoryGame = {};

// accedo.memoryGame.util.time = {
// countTimeNum : 0,
// timeOutID : null,
// timeCount : function() {
// document.getElementById('timeTextID').value = this.countTimeNum;
// this.countTimeNum = this.countTimeNum + 1;
// this.timeOutID = setTimeout("accedo.memoryGame.util.time.timeCount()", 1000);
// }
// };
MemoryGame.Misc = {
	randomArray : [],
	imgNodeforButton : null,
	tempNodeforButton : null,
	score : 0,
	username : null
};
MemoryGame.Control = {
	realNum : null,
	lockEnter : false,
	lockEnterTwoTimes : false,
	stageNum : 0,
	finishFlag : 0,
	lockRight : false
};

MemoryGame.Card = {
	firstCardNum : null,
	firstTempHTML : null,
	firstTdNode : null,
	secondTempHTML : null,
	secondTdNode : null
};

MemoryGame.Board = {
	rowlength : 2,
	collength : 2
};

window.onload = function initGame() {
	MemoryGame.Control.stageNum = 1; // init stage number to 1
	createStage(MemoryGame.Control.stageNum); // create stage logo table
	setTimeout(
			"createTable(210, 135, MemoryGame.Board.rowlength, MemoryGame.Board.rowlength)",
			2500); // create stage 1 main table

	// Parse the URL parameters to get MemoryGame.Misc.username
	var args = new Object();
	args = accedo.memoryGame.util.GetUrlParms();
	MemoryGame.Misc.username = args["username"];

	setTimeout("accedo.memoryGame.util.time.timeCount()", 2200); // start
	// time
	// counting
	// accedo.Achievement.triggerHiScore(); // start HiScore refrashing
	// alert(MemoryGame.Misc.username);
	accedo.Achievement.triggerCardRecord(MemoryGame.Misc.username);
};

// create the number of stage logo
function createStage(num) {
	var tableNode = document.createElement("table");
	tableNode.id = "stageTableID";
	tableNode.style.tableLayout = "fixed";
	var trNode = tableNode.insertRow();
	var tdNode = trNode.insertCell();
	tdNode.align = "center";
	tdNode.width = 400;
	tdNode.height = 300;
	tdNode.innerHTML = "<font class='stageText'><B>STAGE " + num
			+ "</B></font>";
	var divNode = document.getElementById("mainDivID");
	var divChildNode = divNode.childNodes[0];
	if (divChildNode != null) {
		divNode.removeChild(divChildNode);
	}
	divNode.appendChild(tableNode);
	$(divNode).fadeOut(2500);
}

function createTable(cellWidth, cellHeight, f_rowlength, f_collength) {
	MemoryGame.Control.finishFlag = 0; // used to judge whether the stage is
	// clear

	// return the time to 0 and display in the relevant place
	accedo.memoryGame.util.time.countTimeNum = 0;
	// countTimeNum = 0;
	// document.getElementById('timeTextID').value = countTimeNum;
	document.getElementById('timeTextID').value = accedo.memoryGame.util.time.countTimeNum;
	MemoryGame.Board.rowlength = f_rowlength;
	MemoryGame.Board.collength = f_collength;

	// create inner table to contain the cards
	var tableNode = document.createElement("table");
	tableNode.id = "innerTableID";
	tableNode.style.tableLayout = "fixed";

	// create cells of the inner table
	for ( var rowNum = 0; rowNum < f_rowlength; rowNum++) {
		var trNode = tableNode.insertRow();

		for ( var cellNum = 0; cellNum < f_collength; cellNum++) {
			var tdNode = trNode.insertCell();
			tdNode.align = "center";
			tdNode.width = cellWidth;
			tdNode.height = cellHeight;
			tdNode.innerHTML = "<td><img width=80% height=80% id='" + rowNum
					+ cellNum
					+ "' src='Images/card.png' onclick='clickCard()' /></td>";
		}
	}

	// append the inner table to the web page
	var divNode = document.getElementById("mainDivID");
	var divChildNode = divNode.childNodes[0];
	if (divChildNode != null) {
		divNode.removeChild(divChildNode);
	}
	divNode.appendChild(tableNode);
	$(divNode).fadeIn("slow");

	// create a array with random pair number
	MemoryGame.Misc.randomArray = accedo.memoryGame.util.randomArr(f_rowlength,
			f_collength);

	// set the navigator cursor on the top left cell
	MemoryGame.Misc.imgNodeforButton = document
			.getElementById((f_rowlength - 1) + "" + (f_collength - 1));
	MemoryGame.Misc.imgNodeforButton.border = 3;
	MemoryGame.Misc.imgNodeforButton.style.borderColor = "#FFFF00";

};

// deal with the keys down events
document.onkeydown = function(e) {
	e = window.event || e;
	if (MemoryGame.Control.lockEnter == false) {
		var rowNum = parseInt(MemoryGame.Misc.imgNodeforButton.getAttribute(
				"id").substr(0, 1));
		var colNum = parseInt(MemoryGame.Misc.imgNodeforButton.getAttribute(
				"id").substr(1, 1));
		MemoryGame.Misc.imgNodeforButton.border = 0;
		switch (e.keyCode) {
		// left button
		case 37:
			MemoryGame.Control.lockEnterTwoTimes = false;
			MemoryGame.Control.lockRight = false;
			MemoryGame.Misc.tempNodeforButton = document
					.getElementById("achiImgID");
			MemoryGame.Misc.tempNodeforButton.border = 0;
			MemoryGame.Misc.tempNodeforButton = document
					.getElementById("scoreImgID");
			MemoryGame.Misc.tempNodeforButton.border = 0;
			do {
				if (colNum != MemoryGame.Board.collength - 1) {
					colNum++;
				} else {
					if (rowNum != 0) {
						rowNum--;
					} else {
						rowNum = MemoryGame.Board.rowlength - 1;
					}
					colNum = 0;
				}
				var newID = rowNum + "" + colNum;
				MemoryGame.Misc.imgNodeforButton = document
						.getElementById(newID);
			} while (MemoryGame.Misc.imgNodeforButton == null);
			MemoryGame.Misc.imgNodeforButton.border = 3;
			MemoryGame.Misc.imgNodeforButton.style.borderColor = "#FFFF00";
			break;
		// up button
		case 38:
			if (MemoryGame.Control.lockRight == false) {

				MemoryGame.Control.lockEnterTwoTimes = false;
				do {
					if (rowNum != MemoryGame.Board.rowlength - 1) {
						rowNum++;
					} else {
						if (colNum != 0) {
							colNum--;
						} else {
							colNum = MemoryGame.Board.collength - 1;
						}
						rowNum = 0;
					}
					var newID = rowNum + "" + colNum;
					MemoryGame.Misc.imgNodeforButton = document
							.getElementById(newID);
				} while (MemoryGame.Misc.imgNodeforButton == null);
				MemoryGame.Misc.imgNodeforButton.border = 3;
				MemoryGame.Misc.imgNodeforButton.style.borderColor = "#FFFF00";
				break;
			} else {
				MemoryGame.Misc.tempNodeforButton = document
						.getElementById("scoreImgID");
				MemoryGame.Misc.tempNodeforButton.border = 0;
				MemoryGame.Misc.tempNodeforButton = document
						.getElementById("achiImgID");
				MemoryGame.Misc.tempNodeforButton.border = 3;
				MemoryGame.Misc.tempNodeforButton.style.borderColor = "#FFFF00";
				break;
			}
			// right button
		case 39:
			MemoryGame.Control.lockEnterTwoTimes = false;
			do {
				if (colNum != 0) {
					colNum--;
				} else {
					if (rowNum != MemoryGame.Board.rowlength - 1) {
						rowNum++;
					} else {
						rowNum = 0;
					}
					colNum = MemoryGame.Board.collength - 1;

					// ***********************
					MemoryGame.Control.lockRight = true;
					MemoryGame.Misc.tempNodeforButton = document
							.getElementById("achiImgID");
					MemoryGame.Misc.tempNodeforButton.border = 3;
					MemoryGame.Misc.tempNodeforButton.style.borderColor = "#FFFF00";
					// ***********************
				}
				var newID = rowNum + "" + colNum;
				MemoryGame.Misc.imgNodeforButton = document
						.getElementById(newID);
			} while (MemoryGame.Misc.imgNodeforButton == null);
			if (MemoryGame.Control.lockRight == false) {
				MemoryGame.Misc.imgNodeforButton.border = 3;
				MemoryGame.Misc.imgNodeforButton.style.borderColor = "#FFFF00";
			}

			break;
		// down button
		case 40:
			if (MemoryGame.Control.lockRight != true) {
				// alert("lskdjfkldj");
				MemoryGame.Control.lockEnterTwoTimes = false;
				do {
					if (rowNum != 0) {
						rowNum--;
					} else {
						if (colNum != MemoryGame.Board.collength - 1) {
							colNum++;
						} else {
							colNum = 0;
						}
						rowNum = MemoryGame.Board.rowlength - 1;
					}
					var newID = rowNum + "" + colNum;
					MemoryGame.Misc.imgNodeforButton = document
							.getElementById(newID);
				} while (MemoryGame.Misc.imgNodeforButton == null);
				MemoryGame.Misc.imgNodeforButton.border = 3;
				MemoryGame.Misc.imgNodeforButton.style.borderColor = "#FFFF00";
				break;
			} else {
				MemoryGame.Misc.tempNodeforButton = document
						.getElementById("achiImgID");
				MemoryGame.Misc.tempNodeforButton.border = 0;
				MemoryGame.Misc.tempNodeforButton = document
						.getElementById("scoreImgID");
				MemoryGame.Misc.tempNodeforButton.border = 3;
				MemoryGame.Misc.tempNodeforButton.style.borderColor = "#FFFF00";
				break;
			}
			// enter button
		case 13:
			if (MemoryGame.Control.lockRight == false) {
				if (MemoryGame.Control.lockEnterTwoTimes == false) {
					clickCard();
					MemoryGame.Control.lockEnterTwoTimes = true; // avoid
																	// trigger
					// the enter
					// button
					// event two times in the same cell
				}
				break;
			} else {
				clickButton(); 
				break; 
			}

		default:
			break;
		}
	}
};

function clickButton() {
	if (MemoryGame.Misc.tempNodeforButton.getAttribute("id") == 'achiImgID') {
		window.open("achievement.html?username="+MemoryGame.Misc.username,"");
	} else if (MemoryGame.Misc.tempNodeforButton.getAttribute("id") == 'scoreImgID') {
		window.open("hiscore.html","");
	}
	
};

function clickCard() {
	var imgNode = MemoryGame.Misc.imgNodeforButton;
	var rowNum = parseInt(imgNode.getAttribute("id").substr(0, 1));
	var colNum = parseInt(imgNode.getAttribute("id").substr(1, 1));
	var tdNode = imgNode.parentNode;
	MemoryGame.Control.realNum = MemoryGame.Misc.randomArray[rowNum][colNum];

	// open the first card
	if (MemoryGame.Card.firstCardNum == null) {
		MemoryGame.Card.firstCardNum = MemoryGame.Control.realNum;
		MemoryGame.Card.firstTdNode = tdNode;
		MemoryGame.Card.firstTempHTML = tdNode.innerHTML;
		tdNode.innerHTML = "<td><img width=80% height=80% src='Images/"
				+ MemoryGame.Control.realNum + ".png' /></td>";
	} else {
		// if the second card is the same as the first card
		if (MemoryGame.Control.realNum == MemoryGame.Card.firstCardNum) {
			if (MemoryGame.Control.realNum == 5) {
				accedo.Achievement.triggerAchi(MemoryGame.Misc.username,
						"ACHI00003");
			}

			MemoryGame.Control.lockEnter = true;
			tdNode.innerHTML = "<img width=80% height=80% src='Images/"
					+ MemoryGame.Control.realNum + ".png' />";
			$(MemoryGame.Card.firstTdNode.childNodes[0]).fadeOut(1000);
			$(tdNode.childNodes[0]).fadeOut(1000);
			MemoryGame.Card.firstTdNode = null;
			MemoryGame.Card.firstCardNum = null;
			MemoryGame.Card.firstTempHTML = null;

			// add MemoryGame.Misc.score calculation
			MemoryGame.Control.finishFlag++;
			MemoryGame.Misc.score = MemoryGame.Misc.score + 20;
			accedo.Achievement.addScore(MemoryGame.Misc.username, 20);

			if (MemoryGame.Misc.score == 80) {
				accedo.Achievement.triggerAchi(MemoryGame.Misc.username,
						"ACHI00002");
			}
			document.getElementById("scoreTextID").value = MemoryGame.Misc.score;

			// Complete one game
			if (MemoryGame.Control.finishFlag == MemoryGame.Board.rowlength
					* MemoryGame.Board.collength / 2) {
				if (MemoryGame.Control.stageNum == 1) {
					accedo.Achievement.triggerAchi(MemoryGame.Misc.username,
							"ACHI00001");
				} else if (MemoryGame.Control.stageNum == 2) {
					accedo.Achievement.triggerAchi(MemoryGame.Misc.username,
							"ACHI00004");
				}
				MemoryGame.Control.stageNum++;
				switch (MemoryGame.Control.stageNum) {
				case 2:
					setTimeout("createStage(MemoryGame.Control.stageNum)", 1000);
					setTimeout("createTable(140, 90, 4, 4)", 2500);
					break;
				case 3:
					setTimeout("createStage(MemoryGame.Control.stageNum)", 1500);
					setTimeout("createTable(126, 81, 6, 6)", 3000);
					break;
				case 4:
					setTimeout("createStage(MemoryGame.Control.stageNum)", 1500);
					setTimeout("createTable(98, 63, 8, 8)", 3000);
					break;
				}

				// clearTimeout(timeOutID);
			}
			MemoryGame.Control.lockEnter = false;

			// if the second card is not the same as the first card
		} else {
			MemoryGame.Control.lockEnter = true; // lock enter button until
			// the two different
			// cards have turned over
			MemoryGame.Card.secondTdNode = tdNode;
			MemoryGame.Card.secondTempHTML = tdNode.innerHTML;
			tdNode.innerHTML = "<td><img width=80% height=80% src='Images/"
					+ MemoryGame.Control.realNum + ".png' /></td>";

			setTimeout("turnOverCard()", 500);
		}

	}

}

// turn over the two different cards
function turnOverCard() {
	MemoryGame.Card.firstTdNode.innerHTML = MemoryGame.Card.firstTempHTML;
	MemoryGame.Card.secondTdNode.innerHTML = MemoryGame.Card.secondTempHTML;
	MemoryGame.Misc.imgNodeforButton = MemoryGame.Card.secondTdNode.childNodes[0];
	MemoryGame.Misc.imgNodeforButton.border = 3;
	MemoryGame.Misc.imgNodeforButton.style.borderColor = "#FFFF00";
	MemoryGame.Card.firstCardNum = null;
	MemoryGame.Card.firstTdNode = null;
	MemoryGame.Card.firstTempHTML = null;
	MemoryGame.Control.lockEnterTwoTimes = false;
	MemoryGame.Control.lockEnter = false;
}
